//
//  JCTabBar.swift
//  SinaWeibo_18
//
//  Created by mac on 15/10/9.
//  Copyright © 2015年 cz. All rights reserved.
//

import UIKit

class JCTabBar: UITabBar {

    //设置点击按钮的闭包
    var plusButtonBlock: (()->())?
    
    var plusButton: UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //初始化 加号按钮
        let plusButton = UIButton()
        //添加点击事件
        plusButton.addTarget(self, action: "plusBtnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        //设置按钮的图片
        plusButton.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        plusButton.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        plusButton.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        plusButton.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        //自动调整大小
        plusButton.sizeToFit()
        //添加到父控件
        addSubview(plusButton)
        //赋值
        self.plusButton = plusButton
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //设置加号按钮的位置
        self.plusButton?.center = CGPointMake(self.centerX, self.height*0.5)
       CGPointMake(self.centerX, self.height * 0.5)
        //设置每一个按钮的宽度
        let childW = self.width/5
        //设置按钮下标
        var index = 0
        for childView in subviews{
            //printLog(childView)
            //过滤加号按钮 布局
            if childView.isKindOfClass(NSClassFromString("UITabBarButton")!){
//                printLog("hahahaha")
                childView.width = childW
                childView.x = CGFloat(index) * childW
                index++
                if index == 2 {
                    index++
                }
            }
            
        }
        
    }
    @objc private func plusBtnDidClick(){
//        print("plusBtnDidClick")
        //判断block
        if let block = plusButtonBlock {
            block()
        }
    }
}
